
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Axios知识点 · 记录技术文档内容</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="zhengyuliang">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-sectionx/sectionx.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="javascript核心知识.html" />
    
    
    <link rel="prev" href="JavaScript的优雅写法.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">CSS</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../CSS/18种CSS命名和书写规范.html">
            
                <a href="../CSS/18种CSS命名和书写规范.html">
            
                    
                    18种CSS命名和书写规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../CSS/灵活运用CSS开发技巧.html">
            
                <a href="../CSS/灵活运用CSS开发技巧.html">
            
                    
                    灵活运用CSS开发技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../CSS/自适应 flex grid.html">
            
                <a href="../CSS/自适应 flex grid.html">
            
                    
                    自适应 Flex Grid
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">HTML</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../HTML/浏览器里的本地数据库：IndexedDB.html">
            
                <a href="../HTML/浏览器里的本地数据库：IndexedDB.html">
            
                    
                    浏览器里的本地数据库：IndexedDB
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Vue</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../Vue/7个有用的Vue开发技巧.html">
            
                <a href="../Vue/7个有用的Vue开发技巧.html">
            
                    
                    7个有用的Vue开发技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../Vue/Vue this.$router.push、replace、go的区别.html">
            
                <a href="../Vue/Vue this.$router.push、replace、go的区别.html">
            
                    
                    Vue This.$Router.Push、Replace、Go的区别
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../Vue/Vue 的父组件和子组件生命周期钩子函数执行顺序？.html">
            
                <a href="../Vue/Vue 的父组件和子组件生命周期钩子函数执行顺序？.html">
            
                    
                    Vue 的父组件和子组件生命周期钩子函数执行顺序？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../Vue/Vue.set和this.$set应用的场景.html">
            
                <a href="../Vue/Vue.set和this.$set应用的场景.html">
            
                    
                    Vue.Set和This.$Set应用的场景
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../Vue/vue高级技巧.html">
            
                <a href="../Vue/vue高级技巧.html">
            
                    
                    Vue高级技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../Vue/从 0 开始手把手带你搭建一套规范的 Vue3.x 项目工程环境.html">
            
                <a href="../Vue/从 0 开始手把手带你搭建一套规范的 Vue3.x 项目工程环境.html">
            
                    
                    从 0 开始手把手带你搭建一套规范的 Vue3.X 项目工程环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../Vue/浅析 vue-router 源码和动态路由权限分配.html">
            
                <a href="../Vue/浅析 vue-router 源码和动态路由权限分配.html">
            
                    
                    浅析 Vue Router 源码和动态路由权限分配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="../Vue/父组件监听到子组件的生命周期.html">
            
                <a href="../Vue/父组件监听到子组件的生命周期.html">
            
                    
                    父组件监听到子组件的生命周期
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="../Vue/理解Vue.mixin，利用Vue.mixin正确的偷懒.html">
            
                <a href="../Vue/理解Vue.mixin，利用Vue.mixin正确的偷懒.html">
            
                    
                    理解Vue.Mixin，利用Vue.Mixin正确的偷懒
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">JavaScript</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="5个技巧让你更好的编写 JavaScript(ES6">
            
                <span>
            
                    
                    5个技巧让你更好的编写 JavaScript(ES6) 中条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="5种常见函数的写法和调用方式.html">
            
                <a href="5种常见函数的写法和调用方式.html">
            
                    
                    5种常见函数的写法和调用方式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="7道javascript题，让你理解基本的javascript.html">
            
                <a href="7道javascript题，让你理解基本的javascript.html">
            
                    
                    7道Javascript题，让你理解基本的Javascript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="Array.from(">
            
                <span>
            
                    
                    Array.From() 五个超好用的用途
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="ES6汇总资料.html">
            
                <a href="ES6汇总资料.html">
            
                    
                    ES6汇总资料
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="JS开发必须知道的41个技巧.html">
            
                <a href="JS开发必须知道的41个技巧.html">
            
                    
                    JS开发必须知道的41个技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="JavaScript之防抖Debounce和节流Throttle.html">
            
                <a href="JavaScript之防抖Debounce和节流Throttle.html">
            
                    
                    JavaScript之防抖Debounce和节流Throttle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="JavaScript数组求并集，交集和差集.html">
            
                <a href="JavaScript数组求并集，交集和差集.html">
            
                    
                    JavaScript数组求并集，交集和差集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="JavaScript的优雅写法.html">
            
                <a href="JavaScript的优雅写法.html">
            
                    
                    JavaScript的优雅写法
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="4.10" data-path="axios知识点.html">
            
                <a href="axios知识点.html">
            
                    
                    Axios知识点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.11" data-path="javascript核心知识.html">
            
                <a href="javascript核心知识.html">
            
                    
                    Javascript核心知识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.12" data-path="this、apply、call、bind.html">
            
                <a href="this、apply、call、bind.html">
            
                    
                    This、Apply、Call、Bind
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.13" data-path="前端js常用的60余种工具方法.html">
            
                <a href="前端js常用的60余种工具方法.html">
            
                    
                    前端Js常用的60余种工具方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.14" data-path="前端异常的捕获与处理.html">
            
                <a href="前端异常的捕获与处理.html">
            
                    
                    前端异常的捕获与处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.15" data-path="在 JavaScript 中交换值的 10 种方法.html">
            
                <a href="在 JavaScript 中交换值的 10 种方法.html">
            
                    
                    在 JavaScript 中交换值的 10 种方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.16" data-path="常用的前端JavaScript方法封装.html">
            
                <a href="常用的前端JavaScript方法封装.html">
            
                    
                    常用的前端JavaScript方法封装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.17" data-path="异步神器Async-await介绍与填坑.html">
            
                <a href="异步神器Async-await介绍与填坑.html">
            
                    
                    异步神器Async Await介绍与填坑
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.18" data-path="灵活运用JS开发技巧.html">
            
                <a href="灵活运用JS开发技巧.html">
            
                    
                    灵活运用JS开发技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.19" data-path="编写高质量可维护的代码：一目了然的注释.html">
            
                <a href="编写高质量可维护的代码：一目了然的注释.html">
            
                    
                    编写高质量可维护的代码：一目了然的注释
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.20" data-path="编写高质量可维护的代码：优化逻辑判断.html">
            
                <a href="编写高质量可维护的代码：优化逻辑判断.html">
            
                    
                    编写高质量可维护的代码：优化逻辑判断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.21" data-path="编写高质量可维护的代码：优雅命名.html">
            
                <a href="编写高质量可维护的代码：优雅命名.html">
            
                    
                    编写高质量可维护的代码：优雅命名
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.22" data-path="编写高质量可维护的代码：数据建模.html">
            
                <a href="编写高质量可维护的代码：数据建模.html">
            
                    
                    编写高质量可维护的代码：数据建模
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Life Exp</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../life_exp/告别平庸的15个小方法.html">
            
                <a href="../life_exp/告别平庸的15个小方法.html">
            
                    
                    告别平庸的15个小方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../life_exp/日志记录.html">
            
                <a href="../life_exp/日志记录.html">
            
                    
                    日志记录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../life_exp/程序员的发展方向.html">
            
                <a href="../life_exp/程序员的发展方向.html">
            
                    
                    程序员的发展方向
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../message/">
            
                <a href="../message/">
            
                    
                    Message
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Technology</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../technology/WebSocket.html">
            
                <a href="../technology/WebSocket.html">
            
                    
                    WebSocket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="../technology/npm常用命令大全.html">
            
                <a href="../technology/npm常用命令大全.html">
            
                    
                    Npm常用命令大全
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="../technology/智能照明项目说明.html">
            
                <a href="../technology/智能照明项目说明.html">
            
                    
                    智能照明项目说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="../technology/规范化git的提交信息.html">
            
                <a href="../technology/规范化git的提交信息.html">
            
                    
                    规范化Git的提交信息
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Tutorial</li>
        
        
    
        <li class="chapter " data-level="7.1" data-path="../tutorial/Esmap地图详解.html">
            
                <a href="../tutorial/Esmap地图详解.html">
            
                    
                    Esmap地图详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="../tutorial/npm镜像切换.html">
            
                <a href="../tutorial/npm镜像切换.html">
            
                    
                    Npm镜像切换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="../tutorial/发布npm的教程.html">
            
                <a href="../tutorial/发布npm的教程.html">
            
                    
                    发布Npm的教程
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Axios知识点</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <html><head></head><body><div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#axios&#x77E5;&#x8BC6;&#x70B9;&#x53CA;vue&#x7684;&#x4F7F;&#x7528;axios&#x65B9;&#x5F0F;"><b>1. </b>axios&#x77E5;&#x8BC6;&#x70B9;&#x53CA;vue&#x7684;&#x4F7F;&#x7528;axios&#x65B9;&#x5F0F;</a></li><ul><ul><li><span class="title-icon "></span><a href="#&#x7B80;&#x4ECB;"><b>1.1.1. </b>&#x7B80;&#x4ECB;</a></li><li><span class="title-icon "></span><a href="#&#x7279;&#x6027;"><b>1.1.2. </b>&#x7279;&#x6027;</a></li><li><span class="title-icon "></span><a href="#&#x6D4F;&#x89C8;&#x5668;&#x652F;&#x6301;"><b>1.1.3. </b>&#x6D4F;&#x89C8;&#x5668;&#x652F;&#x6301;</a></li><li><span class="title-icon "></span><a href="#&#x5B89;&#x88C5;"><b>1.1.4. </b>&#x5B89;&#x88C5;</a></li><li><span class="title-icon "></span><a href="#&#x4F7F;&#x7528;&#x4E3E;&#x4F8B;"><b>1.1.5. </b>&#x4F7F;&#x7528;&#x4E3E;&#x4F8B;</a></li><li><span class="title-icon "></span><a href="#&#x5E76;&#x53D1;"><b>1.1.6. </b>&#x5E76;&#x53D1;</a></li><li><span class="title-icon "></span><a href="#&#x521B;&#x5EFA;&#x5B9E;&#x4F8B;"><b>1.1.7. </b>&#x521B;&#x5EFA;&#x5B9E;&#x4F8B;</a></li><li><span class="title-icon "></span><a href="#vue&#x4E2D;&#x4F7F;&#x7528;axios&#x7684;&#x591A;&#x79CD;&#x65B9;&#x5F0F;"><b>1.1.8. </b>vue&#x4E2D;&#x4F7F;&#x7528;axios&#x7684;&#x591A;&#x79CD;&#x65B9;&#x5F0F;</a></li></ul></ul></ul></div><a href="#axios&#x77E5;&#x8BC6;&#x70B9;&#x53CA;vue&#x7684;&#x4F7F;&#x7528;axios&#x65B9;&#x5F0F;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="axios&#x77E5;&#x8BC6;&#x70B9;&#x53CA;vue&#x7684;&#x4F7F;&#x7528;axios&#x65B9;&#x5F0F;"><a name="axios&#x77E5;&#x8BC6;&#x70B9;&#x53CA;vue&#x7684;&#x4F7F;&#x7528;axios&#x65B9;&#x5F0F;" class="anchor-navigation-ex-anchor" href="#axios&#x77E5;&#x8BC6;&#x70B9;&#x53CA;vue&#x7684;&#x4F7F;&#x7528;axios&#x65B9;&#x5F0F;"><i class="fa fa-link" aria-hidden="true"></i></a>1. axios&#x77E5;&#x8BC6;&#x70B9;&#x53CA;vue&#x7684;&#x4F7F;&#x7528;axios&#x65B9;&#x5F0F;</h1>
<h3 id="&#x7B80;&#x4ECB;"><a name="&#x7B80;&#x4ECB;" class="anchor-navigation-ex-anchor" href="#&#x7B80;&#x4ECB;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.1. &#x7B80;&#x4ECB;</h3>
<p> Axios &#x662F;&#x4E00;&#x4E2A;&#x57FA;&#x4E8E; <strong>Promise</strong> &#x7684; HTTP &#x5E93;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x548C; node.js &#x4E2D; &#xFF0C;&#x5B83;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x66F4;&#x5F3A;&#x5927;&#x3001;&#x66F4;&#x7075;&#x6D3B;&#x7684;&#x529F;&#x80FD;&#x96C6;&#x3002; </p>
<h3 id="&#x7279;&#x6027;"><a name="&#x7279;&#x6027;" class="anchor-navigation-ex-anchor" href="#&#x7279;&#x6027;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.2. &#x7279;&#x6027;</h3>
<ul>
<li>&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x521B;&#x5EFA; XMLHttpRequests </li>
<li>&#x5728; node.js &#x5219;&#x521B;&#x5EFA; http &#x8BF7;&#x6C42;</li>
<li>&#x652F;&#x6301; <strong>Promise</strong> API</li>
<li>&#x652F;&#x6301;&#x62E6;&#x622A;&#x8BF7;&#x6C42;&#x548C;&#x54CD;&#x5E94;</li>
<li>&#x8F6C;&#x6362;&#x8BF7;&#x6C42;&#x548C;&#x54CD;&#x5E94;&#x6570;&#x636E;</li>
<li>&#x53D6;&#x6D88;&#x8BF7;&#x6C42;</li>
<li>&#x81EA;&#x52A8;&#x8F6C;&#x6362; JSON &#x6570;&#x636E;</li>
<li>&#x5BA2;&#x6237;&#x7AEF;&#x652F;&#x6301;&#x9632;&#x5FA1; XSRF</li>
</ul>
<h3 id="&#x6D4F;&#x89C8;&#x5668;&#x652F;&#x6301;"><a name="&#x6D4F;&#x89C8;&#x5668;&#x652F;&#x6301;" class="anchor-navigation-ex-anchor" href="#&#x6D4F;&#x89C8;&#x5668;&#x652F;&#x6301;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.3. &#x6D4F;&#x89C8;&#x5668;&#x652F;&#x6301;</h3>
<p> &#x652F;&#x6301;Chrome&#x3001;&#x706B;&#x72D0;&#x3001;Edge&#x3001;IE8+&#x7B49;&#x6D4F;&#x89C8;&#x5668; </p>
<h3 id="&#x5B89;&#x88C5;"><a name="&#x5B89;&#x88C5;" class="anchor-navigation-ex-anchor" href="#&#x5B89;&#x88C5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.4. &#x5B89;&#x88C5;</h3>
<p> &#x4F7F;&#x7528; npm&#x5B89;&#x88C5;: </p>
<pre><code class="lang-ini">$ npm install axios
</code></pre>
<p> &#x4F7F;&#x7528; bower: </p>
<pre><code class="lang-ini">$ bower install axios
</code></pre>
<p> &#x6216;&#x8005;&#x76F4;&#x63A5;&#x4F7F;&#x7528; cdn: </p>
<pre><code class="lang-javascript">&lt;script src=<span class="hljs-string">&quot;https://unpkg.com/axios/dist/axios.min.js&quot;</span>&gt;<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
</code></pre>
<h3 id="&#x4F7F;&#x7528;&#x4E3E;&#x4F8B;"><a name="&#x4F7F;&#x7528;&#x4E3E;&#x4F8B;" class="anchor-navigation-ex-anchor" href="#&#x4F7F;&#x7528;&#x4E3E;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.5. &#x4F7F;&#x7528;&#x4E3E;&#x4F8B;</h3>
<p> &#x6267;&#x884C; <code>GET</code> &#x8BF7;&#x6C42;  &#x6CE8;&#xFF1A;&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x5565;&#x65B9;&#x6CD5;&#x8BE6;&#x60C5;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x4E3A;&#x7ED9;&#x5B9A; ID &#x7684; user &#x521B;&#x5EFA;&#x8BF7;&#x6C42;</span>
axios.get(<span class="hljs-string">&apos;/user?ID=12345&apos;</span>)
  .then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">response</span>) </span>{
    <span class="hljs-built_in">console</span>.log(response);
  })
  .catch(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
    <span class="hljs-built_in">console</span>.log(error);
  });

<span class="hljs-comment">// GET &#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x653E;&#x5230;params&#x91CC;&#xFF08;&#x63A8;&#x8350;&#xFF09; get&#x4F7F;&#x7528;params&#x53C2;&#x6570;&#x5185;&#x5BB9;</span>
axios.get(<span class="hljs-string">&apos;/user&apos;</span>, {
    <span class="hljs-attr">params</span>: {
      <span class="hljs-attr">ID</span>: <span class="hljs-number">12345</span>
    }
  })
  .then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">response</span>) </span>{
    <span class="hljs-built_in">console</span>.log(response);
  })
  .catch(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
    <span class="hljs-built_in">console</span>.log(error);
  });

<span class="hljs-comment">// &#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;ECMAScript 2017&#x91CC;&#x7684;async/await&#xFF0C;&#x6DFB;&#x52A0; `async` keyword to your outer function/method.</span>
<span class="hljs-comment">//&#x4F7F;&#x7528;async/await&#x4F7F;&#x7528;&#x8BE6;&#x60C5;</span>
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getUser</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> axios.get(<span class="hljs-string">&apos;/user?ID=12345&apos;</span>);
    <span class="hljs-built_in">console</span>.log(response);
  } <span class="hljs-keyword">catch</span> (error) {
    <span class="hljs-built_in">console</span>.error(error);
  }
}
</code></pre>
<blockquote>
<p> async/await &#x662F; ECMAScript 2017&#x65B0;&#x63D0;&#x4F9B;&#x7684;&#x529F;&#x80FD; &#xFF0C;Internet Explorer &#x548C;&#x4E00;&#x4E9B;&#x65E7;&#x7684;&#x6D4F;&#x89C8;&#x5668;&#x5E76;&#x4E0D;&#x652F;&#x6301; </p>
</blockquote>
<p> &#x6267;&#x884C; <code>POST</code> &#x8BF7;&#x6C42; </p>
<pre><code class="lang-javascript"><span class="hljs-comment">//post&#x4F7F;&#x7528;&#x7684;&#x662F;&#x5BF9;&#x8C61;&#xFF0C;&#x8FD9;&#x91CC;&#x4E5F;&#x8981;&#x6CE8;&#x610F;</span>
axios.post(<span class="hljs-string">&apos;/user&apos;</span>, {
    <span class="hljs-attr">firstName</span>: <span class="hljs-string">&apos;Fred&apos;</span>,
    <span class="hljs-attr">lastName</span>: <span class="hljs-string">&apos;Flintstone&apos;</span>
  })
  .then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">response</span>) </span>{
    <span class="hljs-built_in">console</span>.log(response);
  })
  .catch(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
    <span class="hljs-built_in">console</span>.log(error);
  });
</code></pre>
<p> &#x6267;&#x884C;&#x591A;&#x4E2A;&#x5E76;&#x53D1;&#x8BF7;&#x6C42; axios.all</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//&#x6CE8;&#x610F;axios.all&#x7684;&#x4F7F;&#x7528;&#x6D41;&#x7A0B;&#x7B49;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getUserAccount</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> axios.get(<span class="hljs-string">&apos;/user/12345&apos;</span>);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getUserPermissions</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> axios.get(<span class="hljs-string">&apos;/user/12345/permissions&apos;</span>);
}
axios.all([getUserAccount(), getUserPermissions()])
  .then(axios.spread(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">acct, perms</span>) </span>{
    <span class="hljs-comment">// &#x4E24;&#x4E2A;&#x8BF7;&#x6C42;&#x73B0;&#x5728;&#x90FD;&#x6267;&#x884C;&#x5B8C;&#x6210;</span>
  }));
</code></pre>
<h4 id="axios-api"><a name="axios-api" class="anchor-navigation-ex-anchor" href="#axios-api"><i class="fa fa-link" aria-hidden="true"></i></a>axios API</h4>
<p> &#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5411; <code>axios</code> &#x4F20;&#x9012;&#x76F8;&#x5173;&#x914D;&#x7F6E;&#x6765;&#x521B;&#x5EFA;&#x8BF7;&#x6C42; &#x3002;<strong>&#x8BF7;&#x6C42;&#x56FE;&#x7247;&#x8981;&#x8003;&#x8651;&#x4E0B;&#x4E3A;&#x4EC0;&#x4E48;&#xFF0C;&#x662F;&#x4EC0;&#x4E48;&#x539F;&#x56E0;&#xFF1F;</strong></p>
<h5 id="axiosconfig"><a name="axiosconfig" class="anchor-navigation-ex-anchor" href="#axiosconfig"><i class="fa fa-link" aria-hidden="true"></i></a>axios(config)</h5>
<ul>
<li><pre><code class="lang-javascript"><span class="hljs-comment">// &#x53D1;&#x9001; POST &#x8BF7;&#x6C42;</span>
axios({
  <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;post&apos;</span>,
  <span class="hljs-attr">url</span>: <span class="hljs-string">&apos;/user/12345&apos;</span>,
  <span class="hljs-attr">data</span>: {
    <span class="hljs-attr">firstName</span>: <span class="hljs-string">&apos;Fred&apos;</span>,
    <span class="hljs-attr">lastName</span>: <span class="hljs-string">&apos;Flintstone&apos;</span>
  }
});
<span class="hljs-comment">//  GET &#x8BF7;&#x6C42;&#x8FDC;&#x7A0B;&#x56FE;&#x7247; response&#x53C2;&#x6570;</span>
axios({
  <span class="hljs-attr">method</span>:<span class="hljs-string">&apos;get&apos;</span>,
  <span class="hljs-attr">url</span>:<span class="hljs-string">&apos;http://bit.ly/2mTM3nY&apos;</span>,
  <span class="hljs-attr">responseType</span>:<span class="hljs-string">&apos;stream&apos;</span>
})
  .then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>) </span>{
  response.data.pipe(fs.createWriteStream(<span class="hljs-string">&apos;ada_lovelace.jpg&apos;</span>))
});
</code></pre>
</li>
</ul>
<h5 id="axiosurl-config"><a name="axiosurl-config" class="anchor-navigation-ex-anchor" href="#axiosurl-config"><i class="fa fa-link" aria-hidden="true"></i></a>axios(url[, config])</h5>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x53D1;&#x9001; GET &#x8BF7;&#x6C42;&#xFF08;&#x9ED8;&#x8BA4;&#x7684;&#x65B9;&#x6CD5;&#xFF09;</span>
axios(<span class="hljs-string">&apos;/user/12345&apos;</span>);
</code></pre>
<h4 id="&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x7684;&#x522B;&#x540D;"><a name="&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x7684;&#x522B;&#x540D;" class="anchor-navigation-ex-anchor" href="#&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x7684;&#x522B;&#x540D;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x7684;&#x522B;&#x540D;</h4>
<p> &#x4E3A;&#x65B9;&#x4FBF;&#x4F7F;&#x7528;&#xFF0C;&#x5B98;&#x65B9;&#x4E3A;&#x6240;&#x6709;&#x652F;&#x6301;&#x7684;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x63D0;&#x4F9B;&#x4E86;&#x522B;&#x540D;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x522B;&#x540D;&#x6765;&#x53D1;&#x8D77;&#x8BF7;&#x6C42;&#xFF1A; </p>
<ul>
<li><strong>axios.request(config)</strong></li>
<li><strong>axios.get(url[, config])</strong></li>
<li><strong>axios.delete(url[, config])</strong></li>
<li><strong>axios.head(url[, config])</strong></li>
<li><strong>axios.post(url[, data[, config]])</strong></li>
<li><strong>axios.put(url[, data[, config]])</strong></li>
<li><strong>axios.patch(url[, data[, config]])</strong></li>
</ul>
<h3 id="&#x5E76;&#x53D1;"><a name="&#x5E76;&#x53D1;" class="anchor-navigation-ex-anchor" href="#&#x5E76;&#x53D1;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.6. &#x5E76;&#x53D1;</h3>
<p>&#x5904;&#x7406;&#x5E76;&#x53D1;&#x8BF7;&#x6C42;&#x7684;&#x52A9;&#x624B;&#x51FD;&#x6570;</p>
<ul>
<li><strong>axios.all(iterable)</strong></li>
<li><strong>axios.spread(callback)</strong></li>
</ul>
<h3 id="&#x521B;&#x5EFA;&#x5B9E;&#x4F8B;"><a name="&#x521B;&#x5EFA;&#x5B9E;&#x4F8B;" class="anchor-navigation-ex-anchor" href="#&#x521B;&#x5EFA;&#x5B9E;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.7. &#x521B;&#x5EFA;&#x5B9E;&#x4F8B;</h3>
<p> &#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x81EA;&#x5B9A;&#x4E49;&#x914D;&#x7F6E;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A; axios &#x5B9E;&#x4F8B; </p>
<h4 id="axioscreateconfig"><a name="axioscreateconfig" class="anchor-navigation-ex-anchor" href="#axioscreateconfig"><i class="fa fa-link" aria-hidden="true"></i></a>axios.create([config])</h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> instance = axios.create({
  <span class="hljs-attr">baseURL</span>: <span class="hljs-string">&apos;https://some-domain.com/api/&apos;</span>,
  <span class="hljs-attr">timeout</span>: <span class="hljs-number">1000</span>,
  <span class="hljs-attr">headers</span>: {<span class="hljs-string">&apos;X-Custom-Header&apos;</span>: <span class="hljs-string">&apos;foobar&apos;</span>}
});
</code></pre>
<h4 id="&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;"><a name="&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;</h4>
<p> &#x4EE5;&#x4E0B;&#x662F;&#x53EF;&#x7528;&#x7684;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x3002;&#x6307;&#x5B9A;&#x7684;&#x914D;&#x7F6E;&#x5C06;&#x4E0E;&#x5B9E;&#x4F8B;&#x7684;&#x914D;&#x7F6E;&#x5408;&#x5E76; </p>
<ul>
<li><strong>axios#request(config)</strong></li>
<li><strong>axios#get(url[, config])</strong></li>
<li><strong>axios#delete(url[, config])</strong></li>
<li><strong>axios#head(url[, config])</strong></li>
<li><strong>axios#post(url[, data[, config]])</strong></li>
<li><strong>axios#put(url[, data[, config]])</strong></li>
<li><strong>axios#patch(url[, data[, config]])</strong></li>
</ul>
<h4 id="&#x8BF7;&#x6C42;&#x914D;&#x7F6E;&#x9879;"><a name="&#x8BF7;&#x6C42;&#x914D;&#x7F6E;&#x9879;" class="anchor-navigation-ex-anchor" href="#&#x8BF7;&#x6C42;&#x914D;&#x7F6E;&#x9879;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8BF7;&#x6C42;&#x914D;&#x7F6E;&#x9879;</h4>
<p> &#x4E0B;&#x9762;&#x662F;&#x521B;&#x5EFA;&#x8BF7;&#x6C42;&#x65F6;&#x53EF;&#x7528;&#x7684;&#x914D;&#x7F6E;&#x9009;&#x9879;&#xFF0C;&#x6CE8;&#x610F;&#x53EA;&#x6709; <code>url</code> &#x662F;&#x5FC5;&#x9700;&#x7684;&#x3002;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x6307;&#x5B9A; <code>method</code>&#xFF0C;&#x8BF7;&#x6C42;&#x5C06;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528; <code>get</code> &#x65B9;&#x6CD5;&#x3002; </p>
<pre><code class="lang-javascript">{
  <span class="hljs-comment">// `url` &#x662F;&#x7528;&#x4E8E;&#x8BF7;&#x6C42;&#x7684;&#x670D;&#x52A1;&#x5668; URL</span>
  url: <span class="hljs-string">&quot;/user&quot;</span>,

  <span class="hljs-comment">// `method` &#x662F;&#x521B;&#x5EFA;&#x8BF7;&#x6C42;&#x65F6;&#x4F7F;&#x7528;&#x7684;&#x65B9;&#x6CD5;</span>
  method: <span class="hljs-string">&quot;get&quot;</span>, <span class="hljs-comment">// &#x9ED8;&#x8BA4;&#x662F; get</span>

  <span class="hljs-comment">// `baseURL` &#x5C06;&#x81EA;&#x52A8;&#x52A0;&#x5728; `url` &#x524D;&#x9762;&#xFF0C;&#x9664;&#x975E; `url` &#x662F;&#x4E00;&#x4E2A;&#x7EDD;&#x5BF9; URL&#x3002;</span>
  <span class="hljs-comment">// &#x5B83;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A; `baseURL` &#x4FBF;&#x4E8E;&#x4E3A; axios &#x5B9E;&#x4F8B;&#x7684;&#x65B9;&#x6CD5;&#x4F20;&#x9012;&#x76F8;&#x5BF9; URL</span>
  baseURL: <span class="hljs-string">&quot;https://some-domain.com/api/&quot;</span>,

  <span class="hljs-comment">// `transformRequest` &#x5141;&#x8BB8;&#x5728;&#x5411;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;&#x524D;&#xFF0C;&#x4FEE;&#x6539;&#x8BF7;&#x6C42;&#x6570;&#x636E;</span>
  <span class="hljs-comment">// &#x53EA;&#x80FD;&#x7528;&#x5728; &quot;PUT&quot;, &quot;POST&quot; &#x548C; &quot;PATCH&quot; &#x8FD9;&#x51E0;&#x4E2A;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;</span>
  <span class="hljs-comment">// &#x540E;&#x9762;&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x51FD;&#x6570;&#x5FC5;&#x987B;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x6216; ArrayBuffer&#xFF0C;&#x6216; Stream</span>
  transformRequest: [<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
    <span class="hljs-comment">// &#x5BF9; data &#x8FDB;&#x884C;&#x4EFB;&#x610F;&#x8F6C;&#x6362;&#x5904;&#x7406;</span>

    <span class="hljs-keyword">return</span> data;
  }],

  <span class="hljs-comment">// `transformResponse` &#x5728;&#x4F20;&#x9012;&#x7ED9; then/catch &#x524D;&#xFF0C;&#x5141;&#x8BB8;&#x4FEE;&#x6539;&#x54CD;&#x5E94;&#x6570;&#x636E;</span>
  transformResponse: [<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
    <span class="hljs-comment">// &#x5BF9; data &#x8FDB;&#x884C;&#x4EFB;&#x610F;&#x8F6C;&#x6362;&#x5904;&#x7406;</span>

    <span class="hljs-keyword">return</span> data;
  }],

  <span class="hljs-comment">// `headers` &#x662F;&#x5373;&#x5C06;&#x88AB;&#x53D1;&#x9001;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x8BF7;&#x6C42;&#x5934;</span>
  headers: {<span class="hljs-string">&quot;X-Requested-With&quot;</span>: <span class="hljs-string">&quot;XMLHttpRequest&quot;</span>},

  <span class="hljs-comment">// `params` &#x662F;&#x5373;&#x5C06;&#x4E0E;&#x8BF7;&#x6C42;&#x4E00;&#x8D77;&#x53D1;&#x9001;&#x7684; URL &#x53C2;&#x6570;</span>
  <span class="hljs-comment">// &#x5FC5;&#x987B;&#x662F;&#x4E00;&#x4E2A;&#x65E0;&#x683C;&#x5F0F;&#x5BF9;&#x8C61;(plain object)&#x6216; URLSearchParams &#x5BF9;&#x8C61;</span>
  params: {
    <span class="hljs-attr">ID</span>: <span class="hljs-number">12345</span>
  },

  <span class="hljs-comment">// `paramsSerializer` &#x662F;&#x4E00;&#x4E2A;&#x8D1F;&#x8D23; `params` &#x5E8F;&#x5217;&#x5316;&#x7684;&#x51FD;&#x6570;</span>
  <span class="hljs-comment">// (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)</span>
  paramsSerializer: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">params</span>) </span>{
    <span class="hljs-keyword">return</span> Qs.stringify(params, {<span class="hljs-attr">arrayFormat</span>: <span class="hljs-string">&quot;brackets&quot;</span>})
  },

  <span class="hljs-comment">// `data` &#x662F;&#x4F5C;&#x4E3A;&#x8BF7;&#x6C42;&#x4E3B;&#x4F53;&#x88AB;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;</span>
  <span class="hljs-comment">// &#x53EA;&#x9002;&#x7528;&#x4E8E;&#x8FD9;&#x4E9B;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5; &quot;PUT&quot;, &quot;POST&quot;, &#x548C; &quot;PATCH&quot;</span>
  <span class="hljs-comment">// &#x5728;&#x6CA1;&#x6709;&#x8BBE;&#x7F6E; `transformRequest` &#x65F6;&#xFF0C;&#x5FC5;&#x987B;&#x662F;&#x4EE5;&#x4E0B;&#x7C7B;&#x578B;&#x4E4B;&#x4E00;&#xFF1A;</span>
  <span class="hljs-comment">// - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams</span>
  <span class="hljs-comment">// - &#x6D4F;&#x89C8;&#x5668;&#x4E13;&#x5C5E;&#xFF1A;FormData, File, Blob</span>
  <span class="hljs-comment">// - Node &#x4E13;&#x5C5E;&#xFF1A; Stream</span>
  data: {
    <span class="hljs-attr">firstName</span>: <span class="hljs-string">&quot;Fred&quot;</span>
  },

  <span class="hljs-comment">// `timeout` &#x6307;&#x5B9A;&#x8BF7;&#x6C42;&#x8D85;&#x65F6;&#x7684;&#x6BEB;&#x79D2;&#x6570;(0 &#x8868;&#x793A;&#x65E0;&#x8D85;&#x65F6;&#x65F6;&#x95F4;)</span>
  <span class="hljs-comment">// &#x5982;&#x679C;&#x8BF7;&#x6C42;&#x8BDD;&#x8D39;&#x4E86;&#x8D85;&#x8FC7; `timeout` &#x7684;&#x65F6;&#x95F4;&#xFF0C;&#x8BF7;&#x6C42;&#x5C06;&#x88AB;&#x4E2D;&#x65AD;</span>
  timeout: <span class="hljs-number">1000</span>,

  <span class="hljs-comment">// `withCredentials` &#x8868;&#x793A;&#x8DE8;&#x57DF;&#x8BF7;&#x6C42;&#x65F6;&#x662F;&#x5426;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x51ED;&#x8BC1;</span>
  withCredentials: <span class="hljs-literal">false</span>, <span class="hljs-comment">// &#x9ED8;&#x8BA4;&#x7684;</span>

  <span class="hljs-comment">// `adapter` &#x5141;&#x8BB8;&#x81EA;&#x5B9A;&#x4E49;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#xFF0C;&#x4EE5;&#x4F7F;&#x6D4B;&#x8BD5;&#x66F4;&#x8F7B;&#x677E;</span>
  <span class="hljs-comment">// &#x8FD4;&#x56DE;&#x4E00;&#x4E2A; promise &#x5E76;&#x5E94;&#x7528;&#x4E00;&#x4E2A;&#x6709;&#x6548;&#x7684;&#x54CD;&#x5E94; (&#x67E5;&#x9605; [response docs](#response-api)).</span>
  adapter: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">config</span>) </span>{
    <span class="hljs-comment">/* ... */</span>
  },

  <span class="hljs-comment">// `auth` &#x8868;&#x793A;&#x5E94;&#x8BE5;&#x4F7F;&#x7528; HTTP &#x57FA;&#x7840;&#x9A8C;&#x8BC1;&#xFF0C;&#x5E76;&#x63D0;&#x4F9B;&#x51ED;&#x636E;</span>
  <span class="hljs-comment">// &#x8FD9;&#x5C06;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A; `Authorization` &#x5934;&#xFF0C;&#x8986;&#x5199;&#x6389;&#x73B0;&#x6709;&#x7684;&#x4EFB;&#x610F;&#x4F7F;&#x7528; `headers` &#x8BBE;&#x7F6E;&#x7684;&#x81EA;&#x5B9A;&#x4E49; `Authorization`&#x5934;</span>
  auth: {
    <span class="hljs-attr">username</span>: <span class="hljs-string">&quot;janedoe&quot;</span>,
    <span class="hljs-attr">password</span>: <span class="hljs-string">&quot;s00pers3cret&quot;</span>
  },

  <span class="hljs-comment">// `responseType` &#x8868;&#x793A;&#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x53EF;&#x4EE5;&#x662F; &quot;arraybuffer&quot;, &quot;blob&quot;, &quot;document&quot;, &quot;json&quot;, &quot;text&quot;, &quot;stream&quot;</span>
  responseType: <span class="hljs-string">&quot;json&quot;</span>, <span class="hljs-comment">// &#x9ED8;&#x8BA4;&#x7684;</span>

  <span class="hljs-comment">// `xsrfCookieName` &#x662F;&#x7528;&#x4F5C; xsrf token &#x7684;&#x503C;&#x7684;cookie&#x7684;&#x540D;&#x79F0;</span>
  xsrfCookieName: <span class="hljs-string">&quot;XSRF-TOKEN&quot;</span>, <span class="hljs-comment">// default</span>

  <span class="hljs-comment">// `xsrfHeaderName` &#x662F;&#x627F;&#x8F7D; xsrf token &#x7684;&#x503C;&#x7684; HTTP &#x5934;&#x7684;&#x540D;&#x79F0;</span>
  xsrfHeaderName: <span class="hljs-string">&quot;X-XSRF-TOKEN&quot;</span>, <span class="hljs-comment">// &#x9ED8;&#x8BA4;&#x7684;</span>

  <span class="hljs-comment">// `onUploadProgress` &#x5141;&#x8BB8;&#x4E3A;&#x4E0A;&#x4F20;&#x5904;&#x7406;&#x8FDB;&#x5EA6;&#x4E8B;&#x4EF6;</span>
  onUploadProgress: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">progressEvent</span>) </span>{
    <span class="hljs-comment">// &#x5BF9;&#x539F;&#x751F;&#x8FDB;&#x5EA6;&#x4E8B;&#x4EF6;&#x7684;&#x5904;&#x7406;</span>
  },

  <span class="hljs-comment">// `onDownloadProgress` &#x5141;&#x8BB8;&#x4E3A;&#x4E0B;&#x8F7D;&#x5904;&#x7406;&#x8FDB;&#x5EA6;&#x4E8B;&#x4EF6;</span>
  onDownloadProgress: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">progressEvent</span>) </span>{
    <span class="hljs-comment">// &#x5BF9;&#x539F;&#x751F;&#x8FDB;&#x5EA6;&#x4E8B;&#x4EF6;&#x7684;&#x5904;&#x7406;</span>
  },

  <span class="hljs-comment">// `maxContentLength` &#x5B9A;&#x4E49;&#x5141;&#x8BB8;&#x7684;&#x54CD;&#x5E94;&#x5185;&#x5BB9;&#x7684;&#x6700;&#x5927;&#x5C3A;&#x5BF8;</span>
  maxContentLength: <span class="hljs-number">2000</span>,

  <span class="hljs-comment">// `validateStatus` &#x5B9A;&#x4E49;&#x5BF9;&#x4E8E;&#x7ED9;&#x5B9A;&#x7684;HTTP &#x54CD;&#x5E94;&#x72B6;&#x6001;&#x7801;&#x662F; resolve &#x6216; reject  promise &#x3002;&#x5982;&#x679C; `validateStatus` &#x8FD4;&#x56DE; `true` (&#x6216;&#x8005;&#x8BBE;&#x7F6E;&#x4E3A; `null` &#x6216; `undefined`)&#xFF0C;promise &#x5C06;&#x88AB; resolve; &#x5426;&#x5219;&#xFF0C;promise &#x5C06;&#x88AB; rejecte</span>
  validateStatus: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">status</span>) </span>{
    <span class="hljs-keyword">return</span> status &amp;gt;= <span class="hljs-number">200</span> &amp;amp;&amp;amp; status &amp;lt; <span class="hljs-number">300</span>; <span class="hljs-comment">// &#x9ED8;&#x8BA4;&#x7684;</span>
  },

  <span class="hljs-comment">// `maxRedirects` &#x5B9A;&#x4E49;&#x5728; node.js &#x4E2D; follow &#x7684;&#x6700;&#x5927;&#x91CD;&#x5B9A;&#x5411;&#x6570;&#x76EE;</span>
  <span class="hljs-comment">// &#x5982;&#x679C;&#x8BBE;&#x7F6E;&#x4E3A;0&#xFF0C;&#x5C06;&#x4E0D;&#x4F1A; follow &#x4EFB;&#x4F55;&#x91CD;&#x5B9A;&#x5411;</span>
  maxRedirects: <span class="hljs-number">5</span>, <span class="hljs-comment">// &#x9ED8;&#x8BA4;&#x7684;</span>

  <span class="hljs-comment">// `httpAgent` &#x548C; `httpsAgent` &#x5206;&#x522B;&#x5728; node.js &#x4E2D;&#x7528;&#x4E8E;&#x5B9A;&#x4E49;&#x5728;&#x6267;&#x884C; http &#x548C; https &#x65F6;&#x4F7F;&#x7528;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x4EE3;&#x7406;&#x3002;&#x5141;&#x8BB8;&#x50CF;&#x8FD9;&#x6837;&#x914D;&#x7F6E;&#x9009;&#x9879;&#xFF1A;</span>
  <span class="hljs-comment">// `keepAlive` &#x9ED8;&#x8BA4;&#x6CA1;&#x6709;&#x542F;&#x7528;</span>
  httpAgent: <span class="hljs-keyword">new</span> http.Agent({ <span class="hljs-attr">keepAlive</span>: <span class="hljs-literal">true</span> }),
  <span class="hljs-attr">httpsAgent</span>: <span class="hljs-keyword">new</span> https.Agent({ <span class="hljs-attr">keepAlive</span>: <span class="hljs-literal">true</span> }),

  <span class="hljs-comment">// &quot;proxy&quot; &#x5B9A;&#x4E49;&#x4EE3;&#x7406;&#x670D;&#x52A1;&#x5668;&#x7684;&#x4E3B;&#x673A;&#x540D;&#x79F0;&#x548C;&#x7AEF;&#x53E3;</span>
  <span class="hljs-comment">// `auth` &#x8868;&#x793A; HTTP &#x57FA;&#x7840;&#x9A8C;&#x8BC1;&#x5E94;&#x5F53;&#x7528;&#x4E8E;&#x8FDE;&#x63A5;&#x4EE3;&#x7406;&#xFF0C;&#x5E76;&#x63D0;&#x4F9B;&#x51ED;&#x636E;</span>
  <span class="hljs-comment">// &#x8FD9;&#x5C06;&#x4F1A;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A; `Proxy-Authorization` &#x5934;&#xFF0C;&#x8986;&#x5199;&#x6389;&#x5DF2;&#x6709;&#x7684;&#x901A;&#x8FC7;&#x4F7F;&#x7528; `header` &#x8BBE;&#x7F6E;&#x7684;&#x81EA;&#x5B9A;&#x4E49; `Proxy-Authorization` &#x5934;&#x3002;</span>
  proxy: {
    <span class="hljs-attr">host</span>: <span class="hljs-string">&quot;127.0.0.1&quot;</span>,
    <span class="hljs-attr">port</span>: <span class="hljs-number">9000</span>,
    <span class="hljs-attr">auth</span>: : {
      <span class="hljs-attr">username</span>: <span class="hljs-string">&quot;mikeymike&quot;</span>,
      <span class="hljs-attr">password</span>: <span class="hljs-string">&quot;rapunz3l&quot;</span>
    }
  },

  <span class="hljs-comment">// `cancelToken` &#x6307;&#x5B9A;&#x7528;&#x4E8E;&#x53D6;&#x6D88;&#x8BF7;&#x6C42;&#x7684; cancel token</span>
  <span class="hljs-comment">// &#xFF08;&#x67E5;&#x770B;&#x540E;&#x9762;&#x7684; Cancellation &#x8FD9;&#x8282;&#x4E86;&#x89E3;&#x66F4;&#x591A;&#xFF09;</span>
  cancelToken: <span class="hljs-keyword">new</span> CancelToken(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cancel</span>) </span>{
  })
}
</code></pre>
<h4 id="&#x54CD;&#x5E94;&#x7ED3;&#x6784;"><a name="&#x54CD;&#x5E94;&#x7ED3;&#x6784;" class="anchor-navigation-ex-anchor" href="#&#x54CD;&#x5E94;&#x7ED3;&#x6784;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x54CD;&#x5E94;&#x7ED3;&#x6784;</h4>
<p> axios&#x8BF7;&#x6C42;&#x7684;&#x54CD;&#x5E94;&#x5305;&#x542B;&#x4EE5;&#x4E0B;&#x4FE1;&#x606F;&#xFF1A; </p>
<pre><code class="lang-javascript">{
  <span class="hljs-comment">// `data` &#x7531;&#x670D;&#x52A1;&#x5668;&#x63D0;&#x4F9B;&#x7684;&#x54CD;&#x5E94;</span>
  data: {},

  <span class="hljs-comment">// `status`  HTTP &#x72B6;&#x6001;&#x7801;</span>
  status: <span class="hljs-number">200</span>,

  <span class="hljs-comment">// `statusText` &#x6765;&#x81EA;&#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x7684; HTTP &#x72B6;&#x6001;&#x4FE1;&#x606F;</span>
  statusText: <span class="hljs-string">&quot;OK&quot;</span>,

  <span class="hljs-comment">// `headers` &#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x7684;&#x5934;</span>
  headers: {},

  <span class="hljs-comment">// `config` &#x662F;&#x4E3A;&#x8BF7;&#x6C42;&#x63D0;&#x4F9B;&#x7684;&#x914D;&#x7F6E;&#x4FE1;&#x606F;</span>
  config: {}
}
</code></pre>
<p> &#x4F7F;&#x7528; <code>then</code> &#x65F6;&#xFF0C;&#x4F1A;&#x63A5;&#x6536;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x7684;&#x54CD;&#x5E94;&#xFF1A; </p>
<pre><code class="lang-javascript">axios.get(<span class="hljs-string">&quot;/user/12345&quot;</span>)
  .then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>) </span>{
    <span class="hljs-built_in">console</span>.log(response.data);
    <span class="hljs-built_in">console</span>.log(response.status);
    <span class="hljs-built_in">console</span>.log(response.statusText);
    <span class="hljs-built_in">console</span>.log(response.headers);
    <span class="hljs-built_in">console</span>.log(response.config);
  });
</code></pre>
<p> &#x5728;&#x4F7F;&#x7528; <strong>catch</strong> &#x65F6;&#xFF0C;&#x6216;&#x4F20;&#x9012; <strong>rejection callback</strong> &#x4F5C;&#x4E3A; <strong>then</strong> &#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x65F6;&#xFF0C;&#x54CD;&#x5E94;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <strong>error</strong> &#x5BF9;&#x8C61;&#x53EF;&#x88AB;&#x4F7F;&#x7528;&#xFF0C;&#x6B63;&#x5982;&#x5728;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x8FD9;&#x4E00;&#x8282;&#x6240;&#x8BB2;&#x3002; </p>
<h4 id="&#x914D;&#x7F6E;&#x7684;&#x9ED8;&#x8BA4;&#x503C;defaults"><a name="&#x914D;&#x7F6E;&#x7684;&#x9ED8;&#x8BA4;&#x503C;defaults" class="anchor-navigation-ex-anchor" href="#&#x914D;&#x7F6E;&#x7684;&#x9ED8;&#x8BA4;&#x503C;defaults"><i class="fa fa-link" aria-hidden="true"></i></a>&#x914D;&#x7F6E;&#x7684;&#x9ED8;&#x8BA4;&#x503C;/defaults</h4>
<p> &#x4F60;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x5C06;&#x88AB;&#x7528;&#x5728;&#x5404;&#x4E2A;&#x8BF7;&#x6C42;&#x7684;&#x914D;&#x7F6E;&#x9ED8;&#x8BA4;&#x503C; </p>
<h4 id="&#x5168;&#x5C40;&#x7684;-axios-&#x9ED8;&#x8BA4;&#x503C;"><a name="&#x5168;&#x5C40;&#x7684;-axios-&#x9ED8;&#x8BA4;&#x503C;" class="anchor-navigation-ex-anchor" href="#&#x5168;&#x5C40;&#x7684;-axios-&#x9ED8;&#x8BA4;&#x503C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5168;&#x5C40;&#x7684; axios &#x9ED8;&#x8BA4;&#x503C;</h4>
<pre><code class="lang-javascript">axios.defaults.baseURL = <span class="hljs-string">&quot;https://api.example.com&quot;</span>;
axios.defaults.headers.common[<span class="hljs-string">&quot;Authorization&quot;</span>] = AUTH_TOKEN;
axios.defaults.headers.post[<span class="hljs-string">&quot;Content-Type&quot;</span>] = <span class="hljs-string">&quot;application/x-www-form-urlencoded&quot;</span>;
</code></pre>
<h4 id="&#x81EA;&#x5B9A;&#x4E49;&#x5B9E;&#x4F8B;&#x9ED8;&#x8BA4;&#x503C;"><a name="&#x81EA;&#x5B9A;&#x4E49;&#x5B9E;&#x4F8B;&#x9ED8;&#x8BA4;&#x503C;" class="anchor-navigation-ex-anchor" href="#&#x81EA;&#x5B9A;&#x4E49;&#x5B9E;&#x4F8B;&#x9ED8;&#x8BA4;&#x503C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x81EA;&#x5B9A;&#x4E49;&#x5B9E;&#x4F8B;&#x9ED8;&#x8BA4;&#x503C;</h4>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x521B;&#x5EFA;&#x5B9E;&#x4F8B;&#x65F6;&#x8BBE;&#x7F6E;&#x914D;&#x7F6E;&#x7684;&#x9ED8;&#x8BA4;&#x503C;</span>
<span class="hljs-keyword">var</span> instance = axios.create({
  <span class="hljs-attr">baseURL</span>: <span class="hljs-string">&quot;https://api.example.com&quot;</span>
});

<span class="hljs-comment">// &#x5728;&#x5B9E;&#x4F8B;&#x5DF2;&#x521B;&#x5EFA;&#x540E;&#x4FEE;&#x6539;&#x9ED8;&#x8BA4;&#x503C;</span>
instance.defaults.headers.common[<span class="hljs-string">&quot;Authorization&quot;</span>] = AUTH_TOKEN;
</code></pre>
<h4 id="&#x914D;&#x7F6E;&#x7684;&#x4F18;&#x5148;&#x7EA7;"><a name="&#x914D;&#x7F6E;&#x7684;&#x4F18;&#x5148;&#x7EA7;" class="anchor-navigation-ex-anchor" href="#&#x914D;&#x7F6E;&#x7684;&#x4F18;&#x5148;&#x7EA7;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x914D;&#x7F6E;&#x7684;&#x4F18;&#x5148;&#x7EA7;</h4>
<p>&#x914D;&#x7F6E;&#x4F1A;&#x4EE5;&#x4E00;&#x4E2A;&#x4F18;&#x5148;&#x987A;&#x5E8F;&#x8FDB;&#x884C;&#x5408;&#x5E76;&#x3002;</p>
<p>&#x8BF7;&#x6C42;&#x7684;config &gt; &#x5B9E;&#x4F8B;&#x7684; <code>defaults</code> &#x5C5E;&#x6027; &gt; &#x5E93;&#x9ED8;&#x8BA4;&#x503C;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x6DFB;&#x52A0;&#x8BF7;&#x6C42;&#x62E6;&#x622A;&#x5668;</span>
axios.interceptors.request.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">config</span>) </span>{
    <span class="hljs-comment">// &#x5728;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x4E4B;&#x524D;&#x505A;&#x4E9B;&#x4EC0;&#x4E48;</span>
    <span class="hljs-keyword">return</span> config;
  }, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
    <span class="hljs-comment">// &#x5BF9;&#x8BF7;&#x6C42;&#x9519;&#x8BEF;&#x505A;&#x4E9B;&#x4EC0;&#x4E48;</span>
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.reject(error);
  });

<span class="hljs-comment">// &#x6DFB;&#x52A0;&#x54CD;&#x5E94;&#x62E6;&#x622A;&#x5668;</span>
axios.interceptors.response.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">response</span>) </span>{
    <span class="hljs-comment">// &#x5BF9;&#x54CD;&#x5E94;&#x6570;&#x636E;&#x505A;&#x70B9;&#x4EC0;&#x4E48;</span>
    <span class="hljs-keyword">return</span> response;
  }, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
    <span class="hljs-comment">// &#x5BF9;&#x54CD;&#x5E94;&#x9519;&#x8BEF;&#x505A;&#x70B9;&#x4EC0;&#x4E48;</span>
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.reject(error);
  });
</code></pre>
<p> &#x5982;&#x679C;&#x4F60;&#x60F3;&#x5728;&#x7A0D;&#x540E;&#x79FB;&#x9664;&#x62E6;&#x622A;&#x5668;&#xFF0C;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#xFF1A; </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> myInterceptor = axios.interceptors.request.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{<span class="hljs-comment">/*...*/</span>});
axios.interceptors.request.eject(myInterceptor);
</code></pre>
<p> &#x53EF;&#x4EE5;&#x4E3A;&#x81EA;&#x5B9A;&#x4E49; axios &#x5B9E;&#x4F8B;&#x6DFB;&#x52A0;&#x62E6;&#x622A;&#x5668; </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> instance = axios.create();
instance.interceptors.request.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{<span class="hljs-comment">/*...*/</span>});
</code></pre>
<h4 id="&#x9519;&#x8BEF;&#x5904;&#x7406;"><a name="&#x9519;&#x8BEF;&#x5904;&#x7406;" class="anchor-navigation-ex-anchor" href="#&#x9519;&#x8BEF;&#x5904;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9519;&#x8BEF;&#x5904;&#x7406;</h4>
<pre><code class="lang-javascript">axios.get(<span class="hljs-string">&quot;/user/12345&quot;</span>)
  .catch(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
    <span class="hljs-keyword">if</span> (error.response) {
      <span class="hljs-comment">// &#x8BF7;&#x6C42;&#x5DF2;&#x53D1;&#x51FA;&#xFF0C;&#x4F46;&#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x7684;&#x72B6;&#x6001;&#x7801;&#x4E0D;&#x5728; 2xx &#x8303;&#x56F4;&#x5185;</span>
      <span class="hljs-built_in">console</span>.log(error.response.data);
      <span class="hljs-built_in">console</span>.log(error.response.status);
      <span class="hljs-built_in">console</span>.log(error.response.headers);
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-comment">// Something happened in setting up the request that triggered an Error</span>
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Error&quot;</span>, error.message);
    }
    <span class="hljs-built_in">console</span>.log(error.config);
  });
</code></pre>
<p> &#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>validateStatus</code> &#x914D;&#x7F6E;&#x9009;&#x9879;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x81EA;&#x5B9A;&#x4E49; HTTP &#x72B6;&#x6001;&#x7801;&#x7684;&#x9519;&#x8BEF;&#x8303;&#x56F4; </p>
<pre><code class="lang-javascript">axios.get(<span class="hljs-string">&quot;/user/12345&quot;</span>, {
  <span class="hljs-attr">validateStatus</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">status</span>) </span>{
    <span class="hljs-keyword">return</span> status &lt; <span class="hljs-number">500</span>; <span class="hljs-comment">// &#x72B6;&#x6001;&#x7801;&#x5728;&#x5927;&#x4E8E;&#x6216;&#x7B49;&#x4E8E;500&#x65F6;&#x624D;&#x4F1A; reject</span>
  }
})
</code></pre>
<h4 id="&#x53D6;&#x6D88;&#x8BF7;&#x6C42;"><a name="&#x53D6;&#x6D88;&#x8BF7;&#x6C42;" class="anchor-navigation-ex-anchor" href="#&#x53D6;&#x6D88;&#x8BF7;&#x6C42;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x53D6;&#x6D88;&#x8BF7;&#x6C42;</h4>
<p> &#x4F7F;&#x7528; <em>cancel token</em> &#x53D6;&#x6D88;&#x8BF7;&#x6C42; </p>
<blockquote>
<p> Axios &#x7684; cancel token API &#x57FA;&#x4E8E;cancelable promises proposal </p>
</blockquote>
<p> &#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>CancelToken.source</code> &#x5DE5;&#x5382;&#x65B9;&#x6CD5;&#x521B;&#x5EFA; cancel token&#xFF0C;&#x50CF;&#x8FD9;&#x6837;&#xFF1A; </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> CancelToken = axios.CancelToken;
<span class="hljs-keyword">const</span> source = CancelToken.source();

axios.get(<span class="hljs-string">&apos;/user/12345&apos;</span>, {
  <span class="hljs-attr">cancelToken</span>: source.token
}).catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">thrown</span>) </span>{
  <span class="hljs-keyword">if</span> (axios.isCancel(thrown)) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Request canceled&apos;</span>, thrown.message);
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// handle error</span>
  }
});

axios.post(<span class="hljs-string">&apos;/user/12345&apos;</span>, {
  <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;new name&apos;</span>
}, {
  <span class="hljs-attr">cancelToken</span>: source.token
})

<span class="hljs-comment">// cancel the request (the message parameter is optional)</span>
source.cancel(<span class="hljs-string">&apos;Operation canceled by the user.&apos;</span>);
</code></pre>
<p> &#x8FD8;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4F20;&#x9012;&#x4E00;&#x4E2A; executor &#x51FD;&#x6570;&#x5230; <code>CancelToken</code> &#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6765;&#x521B;&#x5EFA; cancel token&#xFF1A; </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> CancelToken = axios.CancelToken;
<span class="hljs-keyword">var</span> cancel;

axios.get(<span class="hljs-string">&quot;/user/12345&quot;</span>, {
  <span class="hljs-attr">cancelToken</span>: <span class="hljs-keyword">new</span> CancelToken(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">executor</span>(<span class="hljs-params">c</span>) </span>{
    <span class="hljs-comment">// executor &#x51FD;&#x6570;&#x63A5;&#x6536;&#x4E00;&#x4E2A; cancel &#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;</span>
    cancel = c;
  })
});

<span class="hljs-comment">// &#x53D6;&#x6D88;&#x8BF7;&#x6C42;</span>
cancel();
</code></pre>
<blockquote>
<p> &#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x540C;&#x4E00;&#x4E2A; cancel token &#x53D6;&#x6D88;&#x591A;&#x4E2A;&#x8BF7;&#x6C42; </p>
</blockquote>
<h4 id="&#x8BF7;&#x6C42;&#x65F6;&#x4F7F;&#x7528;-applicationx-www-form-urlencoded"><a name="&#x8BF7;&#x6C42;&#x65F6;&#x4F7F;&#x7528;-applicationx-www-form-urlencoded" class="anchor-navigation-ex-anchor" href="#&#x8BF7;&#x6C42;&#x65F6;&#x4F7F;&#x7528;-applicationx-www-form-urlencoded"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8BF7;&#x6C42;&#x65F6;&#x4F7F;&#x7528; application/x-www-form-urlencoded</h4>
<p> axios&#x4F1A;&#x9ED8;&#x8BA4;&#x5E8F;&#x5217;&#x5316; JavaScript &#x5BF9;&#x8C61;&#x4E3A; JSON. &#x5982;&#x679C;&#x60F3;&#x4F7F;&#x7528; application/x-www-form-urlencoded &#x683C;&#x5F0F;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E0B;&#x9762;&#x7684;&#x914D;&#x7F6E;. </p>
<h4 id="&#x6D4F;&#x89C8;&#x5668;"><a name="&#x6D4F;&#x89C8;&#x5668;" class="anchor-navigation-ex-anchor" href="#&#x6D4F;&#x89C8;&#x5668;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6D4F;&#x89C8;&#x5668;</h4>
<p> &#x5728;&#x6D4F;&#x89C8;&#x5668;&#x73AF;&#x5883;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; URLSearchParams API :</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> URLSearchParams();
params.append(<span class="hljs-string">&apos;param1&apos;</span>, <span class="hljs-string">&apos;value1&apos;</span>);
params.append(<span class="hljs-string">&apos;param2&apos;</span>, <span class="hljs-string">&apos;value2&apos;</span>);
axios.post(<span class="hljs-string">&apos;/foo&apos;</span>, params);
</code></pre>
<blockquote>
<p> URLSearchParams&#x4E0D;&#x662F;&#x6240;&#x6709;&#x7684;&#x6D4F;&#x89C8;&#x5668;&#x5747;&#x652F;&#x6301; </p>
</blockquote>
<p> &#x9664;&#x6B64;&#x4E4B;&#x5916;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;qs&#x5E93;&#x6765;&#x7F16;&#x7801;&#x6570;&#x636E;: </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> qs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;qs&apos;</span>);
axios.post(<span class="hljs-string">&apos;/foo&apos;</span>, qs.stringify({ <span class="hljs-string">&apos;bar&apos;</span>: <span class="hljs-number">123</span> }));

<span class="hljs-comment">// Or in another way (ES6),</span>

<span class="hljs-keyword">import</span> qs <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;qs&apos;</span>;
<span class="hljs-keyword">const</span> data = { <span class="hljs-string">&apos;bar&apos;</span>: <span class="hljs-number">123</span> };
<span class="hljs-keyword">const</span> options = {
  <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;POST&apos;</span>,
  <span class="hljs-attr">headers</span>: { <span class="hljs-string">&apos;content-type&apos;</span>: <span class="hljs-string">&apos;application/x-www-form-urlencoded&apos;</span> },
  <span class="hljs-attr">data</span>: qs.stringify(data),
  url,
};
axios(options);
</code></pre>
<p> &#x5F53;&#x7136;&#xFF0C;&#x540C;&#x6D4F;&#x89C8;&#x5668;&#x4E00;&#x6837;&#xFF0C;&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; qs library. </p>
<h4 id="promises"><a name="promises" class="anchor-navigation-ex-anchor" href="#promises"><i class="fa fa-link" aria-hidden="true"></i></a>Promises</h4>
<p> axios &#x4F9D;&#x8D56;&#x539F;&#x751F;&#x7684; ES6 Promise &#x5B9E;&#x73B0;&#x800C;&#x88AB;&#x652F;&#x6301;.
&#x5982;&#x679C;&#x4F60;&#x7684;&#x73AF;&#x5883;&#x4E0D;&#x652F;&#x6301; ES6 Promise&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; polyfill. </p>
<h4 id="typescript&#x652F;&#x6301;"><a name="typescript&#x652F;&#x6301;" class="anchor-navigation-ex-anchor" href="#typescript&#x652F;&#x6301;"><i class="fa fa-link" aria-hidden="true"></i></a>TypeScript&#x652F;&#x6301;</h4>
<p> axios &#x5305;&#x542B; TypeScript definitions. </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;axios&quot;</span>;
axios.get(<span class="hljs-string">&quot;/user?ID=12345&quot;</span>);
</code></pre>
<h3 id="vue&#x4E2D;&#x4F7F;&#x7528;axios&#x7684;&#x591A;&#x79CD;&#x65B9;&#x5F0F;"><a name="vue&#x4E2D;&#x4F7F;&#x7528;axios&#x7684;&#x591A;&#x79CD;&#x65B9;&#x5F0F;" class="anchor-navigation-ex-anchor" href="#vue&#x4E2D;&#x4F7F;&#x7528;axios&#x7684;&#x591A;&#x79CD;&#x65B9;&#x5F0F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.8. vue&#x4E2D;&#x4F7F;&#x7528;axios&#x7684;&#x591A;&#x79CD;&#x65B9;&#x5F0F;</h3>
<p> &#x5B89;&#x88C5;&#x5176;&#x4ED6;&#x63D2;&#x4EF6;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5728; main.js &#x4E2D;&#x5F15;&#x5165;&#x5E76; Vue.use()&#xFF0C;&#x4F46;&#x662F; axios &#x5E76;&#x4E0D;&#x80FD; use&#xFF0C;&#x53EA;&#x80FD;&#x6BCF;&#x4E2A;&#x9700;&#x8981;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x7684;&#x7EC4;&#x4EF6;&#x4E2D;&#x5373;&#x65F6;&#x5F15;&#x5165; </p>
<p> &#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x6709;&#x4E24;&#x79CD;&#x5F00;&#x53D1;&#x601D;&#x8DEF;&#xFF0C;&#x4E00;&#x662F;&#x5728;&#x5F15;&#x5165; axios &#x4E4B;&#x540E;&#xFF0C;&#x4FEE;&#x6539;&#x539F;&#x578B;&#x94FE;&#xFF0C;&#x4E8C;&#x662F;&#x7ED3;&#x5408; Vuex&#xFF0C;&#x5C01;&#x88C5;&#x4E00;&#x4E2A; aciton </p>
<p>&#x4F7F;&#x7528;npm</p>
<p>npm install axios</p>
<p>&#x4F7F;&#x7528;cdn:</p>
<p><script src="https://unpkg.com/axios/dist/axios.min.js"></script></p>
<h4 id="&#x89E3;&#x51B3;post&#x65B9;&#x6CD5;&#x4F7F;&#x7528;applicationx-www-form-urlencoded&#x683C;&#x5F0F;&#x7F16;&#x7801;&#x6570;&#x636E;"><a name="&#x89E3;&#x51B3;post&#x65B9;&#x6CD5;&#x4F7F;&#x7528;applicationx-www-form-urlencoded&#x683C;&#x5F0F;&#x7F16;&#x7801;&#x6570;&#x636E;" class="anchor-navigation-ex-anchor" href="#&#x89E3;&#x51B3;post&#x65B9;&#x6CD5;&#x4F7F;&#x7528;applicationx-www-form-urlencoded&#x683C;&#x5F0F;&#x7F16;&#x7801;&#x6570;&#x636E;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x89E3;&#x51B3;post&#x65B9;&#x6CD5;&#x4F7F;&#x7528;application/x-www-form-urlencoded&#x683C;&#x5F0F;&#x7F16;&#x7801;&#x6570;&#x636E;</h4>
<ol>
<li><p>&#x8BBE;&#x7F6E; headers:{ &apos;Content-type&apos;: &apos;application/x-www-form-urlencoded&apos;} </p>
<pre><code class="lang-javascript">axios.post(<span class="hljs-string">&apos;url&apos;</span>,data,{<span class="hljs-attr">headers</span>:{ <span class="hljs-string">&apos;Content-type&apos;</span>: <span class="hljs-string">&apos;application/x-www-form-urlencoded&apos;</span>}})

<span class="hljs-comment">// &#x4E0D;&#x60F3;&#x5728;&#x6BCF;&#x6B21;&#x8BF7;&#x6C42;&#x90FD;&#x8BBE;&#x7F6E;&#x7684;&#x8BDD;&#xFF0C;&#x53EF;&#x4EE5;&#x96C6;&#x4E2D;&#x8BBE;&#x7F6E;&#x4E0B;</span>
axios.defaults.headers.post[<span class="hljs-string">&apos;Content-Type&apos;</span>] = <span class="hljs-string">&apos;application/x-www-form-urlencoded; charset=UTF-8&apos;</span>;
</code></pre>
</li>
</ol>
<ol>
<li><p>&#x4EC5;&#x4EC5;&#x8FD9;&#x6837;&#x5E76;&#x6CA1;&#x6709;&#x8FBE;&#x5230;&#x60F3;&#x8981;&#x7684;&#x6548;&#x679C;&#xFF0C;post&#x7684;body&#x4E3B;&#x4F53;&#x4E2D;&#x8FD8;&#x662F;{&quot;age&quot;:10}&#x8FD9;&#x6837;&#x7684;&#x683C;
&#x5F0F;&#xFF0C;&#x5E76;&#x4E0D;&#x662F;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x7684;query&#x53C2;&#x6570;&#x3002;&#x5F15;&#x5165;Qs&#xFF0C;&#x8FD9;&#x4E2A;&#x5E93;&#x662F;axios&#x91CC;&#x9762;&#x5305;&#x542B;&#x7684;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x518D;&#x4E0B;&#x8F7D;&#x4E86; </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> qs <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;qs&apos;</span>
<span class="hljs-keyword">var</span> data = qs.stringify({<span class="hljs-string">&quot;name&quot;</span>:<span class="hljs-string">&quot;xie&quot;</span>});
axios.post(<span class="hljs-string">&apos;url&apos;</span>,data).then()
</code></pre>
</li>
</ol>
<h4 id="axios&#x9ED8;&#x8BA4;&#x662F;&#x4E0D;&#x8BA9;ajax&#x8BF7;&#x6C42;&#x5934;&#x90E8;&#x643A;&#x5E26;cookie&#x7684;"><a name="axios&#x9ED8;&#x8BA4;&#x662F;&#x4E0D;&#x8BA9;ajax&#x8BF7;&#x6C42;&#x5934;&#x90E8;&#x643A;&#x5E26;cookie&#x7684;" class="anchor-navigation-ex-anchor" href="#axios&#x9ED8;&#x8BA4;&#x662F;&#x4E0D;&#x8BA9;ajax&#x8BF7;&#x6C42;&#x5934;&#x90E8;&#x643A;&#x5E26;cookie&#x7684;"><i class="fa fa-link" aria-hidden="true"></i></a>axios&#x9ED8;&#x8BA4;&#x662F;&#x4E0D;&#x8BA9;ajax&#x8BF7;&#x6C42;&#x5934;&#x90E8;&#x643A;&#x5E26;cookie&#x7684;</h4>
<h4 id="axios-&#x89E3;&#x51B3;&#x8DE8;&#x57DF;cookie&#x4E22;&#x5931;&#x95EE;&#x9898;"><a name="axios-&#x89E3;&#x51B3;&#x8DE8;&#x57DF;cookie&#x4E22;&#x5931;&#x95EE;&#x9898;" class="anchor-navigation-ex-anchor" href="#axios-&#x89E3;&#x51B3;&#x8DE8;&#x57DF;cookie&#x4E22;&#x5931;&#x95EE;&#x9898;"><i class="fa fa-link" aria-hidden="true"></i></a>axios &#x89E3;&#x51B3;&#x8DE8;&#x57DF;cookie&#x4E22;&#x5931;&#x95EE;&#x9898;</h4>
<p> &#x8BBE;&#x7F6E; axios.defaults.withCredentials = true &#x5373;&#x53EF; </p>
<p> &#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A; </p>
<pre><code class="lang-javascript">axios.defaults.withCredentials = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">var</span> param = <span class="hljs-keyword">new</span> URLSearchParams();
param.append(<span class="hljs-string">&quot;vCode&quot;</span>,vcode);
axios.post(<span class="hljs-string">&apos;http://localhost&apos;</span>,param)
    .then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">res</span>) </span>{
    <span class="hljs-keyword">var</span> rs=res.data;
    <span class="hljs-built_in">console</span>.log(rs.data);
})
    .catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err</span>) </span>{
    <span class="hljs-built_in">console</span>.log(err);
});
</code></pre>
<h4 id="&#x914D;&#x5408;vue"><a name="&#x914D;&#x5408;vue" class="anchor-navigation-ex-anchor" href="#&#x914D;&#x5408;vue"><i class="fa fa-link" aria-hidden="true"></i></a>&#x914D;&#x5408;vue</h4>
<blockquote>
<p>axios&#x5E76;&#x6CA1;&#x6709;install &#x65B9;&#x6CD5;&#xFF0C;&#x6240;&#x4EE5;&#x662F;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;vue.use()&#x65B9;&#x6CD5;&#x7684;&#x3002;
 &#x90A3;&#x4E48;&#x96BE;&#x9053;&#x6BCF;&#x4E2A;&#x6587;&#x4EF6;&#x90FD;&#x8981;&#x6765;&#x5F15;&#x7528;&#x4E00;&#x6B21;&#xFF1F;&#x89E3;&#x51B3;&#x65B9;&#x6CD5;&#x6709;&#x5F88;&#x591A;&#x79CD;&#xFF1A;</p>
<ul>
<li>&#x7ED3;&#x5408; vue-axios&#x4F7F;&#x7528;</li>
<li>axios &#x6539;&#x5199;&#x4E3A; Vue &#x7684;&#x539F;&#x578B;&#x5C5E;&#x6027;</li>
<li>&#x7ED3;&#x5408; Vuex&#x7684;action</li>
</ul>
</blockquote>
<h5 id="&#x65B9;&#x6CD5;1&#xFF1A;&#x7ED3;&#x5408;-vue-axios&#x4F7F;&#x7528;"><a name="&#x65B9;&#x6CD5;1&#xFF1A;&#x7ED3;&#x5408;-vue-axios&#x4F7F;&#x7528;" class="anchor-navigation-ex-anchor" href="#&#x65B9;&#x6CD5;1&#xFF1A;&#x7ED3;&#x5408;-vue-axios&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x65B9;&#x6CD5;1&#xFF1A;&#x7ED3;&#x5408; vue-axios&#x4F7F;&#x7528;</h5>
<p>vue-axios</p>
<p>&#x7528;&#x4E8E;&#x5C06;axios&#x96C6;&#x6210;&#x5230;Vuejs&#x7684;&#x5C0F;&#x5305;&#x88C5;&#x5668;</p>
<p>github: <a href="https://github.com/axios/axios" target="_blank">https://github.com/axios/axios</a></p>
<p>&#x5B89;&#x88C5;&#xFF1A; <strong>npm install --save axios vue-axios</strong></p>
<p>vue-axios&#x662F;&#x6309;&#x7167;vue&#x63D2;&#x4EF6;&#x7684;&#x65B9;&#x5F0F;&#x53BB;&#x5199;&#x7684;&#x3002;&#x90A3;&#x4E48;&#x7ED3;&#x5408;vue-axios&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x53BB;&#x4F7F;&#x7528;vue.use&#x65B9;&#x6CD5;&#x4E86;</p>
<pre><code class="lang-javascript">&#x9996;&#x5148;&#x5728;&#x4E3B;&#x5165;&#x53E3;&#x6587;&#x4EF6;main.js&#x4E2D;&#x5F15;&#x7528;

<span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;axios&apos;</span>
<span class="hljs-keyword">import</span> VueAxios <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;vue-axios&apos;</span>

Vue.use(VueAxios,axios);
</code></pre>
<p> &#x4E4B;&#x540E;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E86;&#xFF0C;&#x5728;&#x7EC4;&#x4EF6;&#x6587;&#x4EF6;&#x4E2D;&#x7684;methods&#x91CC;&#x53BB;&#x4F7F;&#x7528;&#x4E86; </p>
<pre><code class="lang-javascript">getNewsList(){
      <span class="hljs-keyword">this</span>.axios.get(<span class="hljs-string">&apos;api/getNewsList&apos;</span>).then(<span class="hljs-function">(<span class="hljs-params">response</span>)=&gt;</span>{
        <span class="hljs-keyword">this</span>.newsList=response.data.data;
      }).catch(<span class="hljs-function">(<span class="hljs-params">response</span>)=&gt;</span>{
        <span class="hljs-built_in">console</span>.log(response);
      })
    },
</code></pre>
<h5 id="&#x65B9;&#x6CD5;2&#xFF1A;-axios-&#x6539;&#x5199;&#x4E3A;-vue-&#x7684;&#x539F;&#x578B;&#x5C5E;&#x6027;"><a name="&#x65B9;&#x6CD5;2&#xFF1A;-axios-&#x6539;&#x5199;&#x4E3A;-vue-&#x7684;&#x539F;&#x578B;&#x5C5E;&#x6027;" class="anchor-navigation-ex-anchor" href="#&#x65B9;&#x6CD5;2&#xFF1A;-axios-&#x6539;&#x5199;&#x4E3A;-vue-&#x7684;&#x539F;&#x578B;&#x5C5E;&#x6027;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x65B9;&#x6CD5;2&#xFF1A; axios &#x6539;&#x5199;&#x4E3A; Vue &#x7684;&#x539F;&#x578B;&#x5C5E;&#x6027;</h5>
<p>&#x9996;&#x5148;&#x5728;&#x4E3B;&#x5165;&#x53E3;&#x6587;&#x4EF6;main.js&#x4E2D;&#x5F15;&#x7528;&#xFF0C;&#x4E4B;&#x540E;&#x6302;&#x5728;vue&#x7684;&#x539F;&#x578B;&#x94FE;&#x4E0A;</p>
<p><strong>import axios from &apos;axios&apos;</strong>
<strong>Vue.prototype.$axios= axios</strong></p>
<p> &#x5728;&#x7EC4;&#x4EF6;&#x4E2D;&#x4F7F;&#x7528; </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">this</span>.$axios.get(<span class="hljs-string">&apos;api/getNewsList&apos;</span>).then(<span class="hljs-function">(<span class="hljs-params">response</span>)=&gt;</span>{
        <span class="hljs-keyword">this</span>.newsList=response.data.data;
      }).catch(<span class="hljs-function">(<span class="hljs-params">response</span>)=&gt;</span>{
        <span class="hljs-built_in">console</span>.log(response);
      })
</code></pre>
<h5 id="&#x65B9;&#x6CD5;3&#xFF1A;&#x7ED3;&#x5408;vuex"><a name="&#x65B9;&#x6CD5;3&#xFF1A;&#x7ED3;&#x5408;vuex" class="anchor-navigation-ex-anchor" href="#&#x65B9;&#x6CD5;3&#xFF1A;&#x7ED3;&#x5408;vuex"><i class="fa fa-link" aria-hidden="true"></i></a>&#x65B9;&#x6CD5;3&#xFF1A;&#x7ED3;&#x5408;vuex</h5>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;axios&apos;</span>
<span class="hljs-keyword">import</span> { Message } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;element-ui&apos;</span>
<span class="hljs-keyword">import</span> store <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;@/store&apos;</span>
<span class="hljs-keyword">import</span> { getToken } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;@/utils/auth&apos;</span>

<span class="hljs-comment">// &#x521B;&#x5EFA;axios&#x5B9E;&#x4F8B;</span>
<span class="hljs-keyword">const</span> service = axios.create({
  <span class="hljs-attr">baseURL</span>: process.env.BASE_API, <span class="hljs-comment">// api&#x7684;base_url</span>
  timeout: <span class="hljs-number">5000</span> <span class="hljs-comment">// &#x8BF7;&#x6C42;&#x8D85;&#x65F6;&#x65F6;&#x95F4;</span>
})

<span class="hljs-comment">// request&#x62E6;&#x622A;&#x5668;</span>
service.interceptors.request.use(<span class="hljs-function"><span class="hljs-params">config</span> =&gt;</span> {
  <span class="hljs-comment">// Do something before request is sent</span>
  <span class="hljs-keyword">if</span> (store.getters.token) {
    config.headers[<span class="hljs-string">&apos;X-Token&apos;</span>] = getToken() <span class="hljs-comment">// &#x8BA9;&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x643A;&#x5E26;token--[&apos;X-Token&apos;]&#x4E3A;&#x81EA;&#x5B9A;&#x4E49;key &#x8BF7;&#x6839;&#x636E;&#x5B9E;&#x9645;&#x60C5;&#x51B5;&#x81EA;&#x884C;&#x4FEE;&#x6539;</span>
  }
  <span class="hljs-keyword">return</span> config
}, error =&gt; {
  <span class="hljs-comment">// Do something with request error</span>
  <span class="hljs-built_in">console</span>.log(error) <span class="hljs-comment">// for debug</span>
  <span class="hljs-built_in">Promise</span>.reject(error)
})

<span class="hljs-comment">// respone&#x62E6;&#x622A;&#x5668;</span>
service.interceptors.response.use(
  <span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> response,
  <span class="hljs-comment">/**
  * &#x4E0B;&#x9762;&#x7684;&#x6CE8;&#x91CA;&#x4E3A;&#x901A;&#x8FC7;response&#x81EA;&#x5B9A;&#x4E49;code&#x6765;&#x6807;&#x793A;&#x8BF7;&#x6C42;&#x72B6;&#x6001;&#xFF0C;&#x5F53;code&#x8FD4;&#x56DE;&#x5982;&#x4E0B;&#x60C5;&#x51B5;&#x4E3A;&#x6743;&#x9650;&#x6709;&#x95EE;&#x9898;&#xFF0C;&#x767B;&#x51FA;&#x5E76;&#x8FD4;&#x56DE;&#x5230;&#x767B;&#x5F55;&#x9875;
  * &#x5982;&#x901A;&#x8FC7;xmlhttprequest &#x72B6;&#x6001;&#x7801;&#x6807;&#x8BC6; &#x903B;&#x8F91;&#x53EF;&#x5199;&#x5728;&#x4E0B;&#x9762;error&#x4E2D;
  */</span>
  <span class="hljs-comment">//  const res = response.data;</span>
  <span class="hljs-comment">//     if (res.code !== 20000) {</span>
  <span class="hljs-comment">//       Message({</span>
  <span class="hljs-comment">//         message: res.message,</span>
  <span class="hljs-comment">//         type: &apos;error&apos;,</span>
  <span class="hljs-comment">//         duration: 5 * 1000</span>
  <span class="hljs-comment">//       });</span>
  <span class="hljs-comment">//       // 50008:&#x975E;&#x6CD5;&#x7684;token; 50012:&#x5176;&#x4ED6;&#x5BA2;&#x6237;&#x7AEF;&#x767B;&#x5F55;&#x4E86;;  50014:Token &#x8FC7;&#x671F;&#x4E86;;</span>
  <span class="hljs-comment">//       if (res.code === 50008 || res.code === 50012 || res.code === 50014) {</span>
  <span class="hljs-comment">//         MessageBox.confirm(&apos;&#x4F60;&#x5DF2;&#x88AB;&#x767B;&#x51FA;&#xFF0C;&#x53EF;&#x4EE5;&#x53D6;&#x6D88;&#x7EE7;&#x7EED;&#x7559;&#x5728;&#x8BE5;&#x9875;&#x9762;&#xFF0C;&#x6216;&#x8005;&#x91CD;&#x65B0;&#x767B;&#x5F55;&apos;, &apos;&#x786E;&#x5B9A;&#x767B;&#x51FA;&apos;, {</span>
  <span class="hljs-comment">//           confirmButtonText: &apos;&#x91CD;&#x65B0;&#x767B;&#x5F55;&apos;,</span>
  <span class="hljs-comment">//           cancelButtonText: &apos;&#x53D6;&#x6D88;&apos;,</span>
  <span class="hljs-comment">//           type: &apos;warning&apos;</span>
  <span class="hljs-comment">//         }).then(() =&gt; {</span>
  <span class="hljs-comment">//           store.dispatch(&apos;FedLogOut&apos;).then(() =&gt; {</span>
  <span class="hljs-comment">//             location.reload();// &#x4E3A;&#x4E86;&#x91CD;&#x65B0;&#x5B9E;&#x4F8B;&#x5316;vue-router&#x5BF9;&#x8C61; &#x907F;&#x514D;bug</span>
  <span class="hljs-comment">//           });</span>
  <span class="hljs-comment">//         })</span>
  <span class="hljs-comment">//       }</span>
  <span class="hljs-comment">//       return Promise.reject(&apos;error&apos;);</span>
  <span class="hljs-comment">//     } else {</span>
  <span class="hljs-comment">//       return response.data;</span>
  <span class="hljs-comment">//     }</span>
  error =&gt; {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;err&apos;</span> + error)<span class="hljs-comment">// for debug</span>
    Message({
      <span class="hljs-attr">message</span>: error.message,
      <span class="hljs-attr">type</span>: <span class="hljs-string">&apos;error&apos;</span>,
      <span class="hljs-attr">duration</span>: <span class="hljs-number">5</span> * <span class="hljs-number">1000</span>
    })
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.reject(error)
  })

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> service
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> request <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;@/utils/request&apos;</span>

<span class="hljs-comment">//&#x4F7F;&#x7528;</span>
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getInfo</span>(<span class="hljs-params">params</span>) </span>{
  <span class="hljs-keyword">return</span> request({
    <span class="hljs-attr">url</span>: <span class="hljs-string">&apos;/user/info&apos;</span>,
    <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;get&apos;</span>,
    params
  });
}
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; zhengyuliang all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x7F16;&#x5199;&#x65F6;&#x95F4;&#xFF1A;
2019-11-14 15:47:03
</span></footer></body></html>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="JavaScript的优雅写法.html" class="navigation navigation-prev " aria-label="Previous page: JavaScript的优雅写法">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="javascript核心知识.html" class="navigation navigation-next " aria-label="Next page: Javascript核心知识">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Axios知识点","level":"4.10","depth":1,"next":{"title":"Javascript核心知识","level":"4.11","depth":1,"path":"javaScript/javascript核心知识.md","ref":"javaScript/javascript核心知识.md","articles":[]},"previous":{"title":"JavaScript的优雅写法","level":"4.9","depth":1,"path":"javaScript/JavaScript的优雅写法.md","ref":"javaScript/JavaScript的优雅写法.md","articles":[]},"dir":"ltr"},"config":{"plugins":["sectionx","splitter","tbfed-pagefooter","toggle-chapters","theme-comscore","-lunr","-search","search-plus","donate","anchor-navigation-ex","expandable-chapters","chapter-fold","expandable-chapters-small","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy zhengyuliang","modify_label":"该文件编写时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"pluginsConfig":{"anchor-navigation-ex":{"showLevel":false,"showGoTop":true,"isRewritePageTitle":true,"isShowTocTitleIcon":true,"tocLevel1Icon":"fa fa-hand-o-right","tocLevel2Icon":"fa fa-hand-o-right","tocLevel3Icon":"fa fa-hand-o-right"}},"livereload":{},"splitter":{},"donate":{"title":"","button":"赏","wechat":"https://gitee.com/htzyl/img_bed/raw/master/pay/weixin-min.jpg","alipay":"https://gitee.com/htzyl/img_bed/raw/master/pay/alipay-min.jpg","wechatText":"微信打赏","alipayText":"支付宝打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"sectionx":{"tag":"b"},"highlight":{},"anchor-navigation-ex":{"showLevel":true,"associatedWithSummary":true,"mode":"float","showGoTop":true,"printLog":false,"multipleH1":true,"float":{"floatIcon":"fa fa-navicon","showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""},"pageTop":{"showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""}},"theme-comscore":{},"expandable-chapters-small":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"toggle-chapters":{},"expandable-chapters":{},"search-plus":{}},"theme":"default","author":"zhengyuliang","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"记录技术文档内容","language":"zh-hans","gitbook":">=3.0.0","description":"记录Gitbook的配置和一些插件的使用","theme-default":{"showLevel":true}},"file":{"path":"javaScript/axios知识点.md","mtime":"2019-11-14T07:47:03.895Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-01-06T09:44:01.178Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sectionx/sectionx.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

